/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chuiniurobot;

import chuiniurobot.message.BaseMessage;
import chuiniurobot.message.GuessTimeOverMessage;
import chuiniurobot.message.LoginMessage;
import chuiniurobot.message.OpenMatchMessage;
import chuiniurobot.message.PKMessage;
import chuiniurobot.message.QuitTeamMessage;
import chuiniurobot.message.ResponsePKMessage;
import chuiniurobot.message.ShakeDiceMessage;
import chuiniurobot.message.StartGuessMessage;
import chuiniurobot.message.VSPlayerAllDiceNumberMessage;
import chuiniurobot.message.VSPlayerGuessNumberMessage;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chuiniu.server.command.BaseCommand;
import org.apache.mina.core.session.IoSession;

/**
 *
 * @author zhengji
 */
public class MSGDispatch {
      private IoSession ioSession;
     private String message;
     ChuiniuRobot robot;
  public MSGDispatch(IoSession ioSession,Object message){
	  this.ioSession=ioSession;
	  this.message=message==null?"":message.toString().trim();
  }
  public void setRobot(ChuiniuRobot robot){
      this.robot=robot;
  }
    public void parseMessage(){
        if(message==null || message.trim().equals("")) return ;
        
        JSONObject jsonObj=  JSON.parseObject(message);
        
        String cmd=jsonObj.getString("cmd");
        
        BaseMessage baseMessage=null;
        if(cmd.equalsIgnoreCase(BaseCommand.RequestPKCommand)){
            //请求对战
            
            baseMessage=new PKMessage(this.ioSession,this.message);
        }else if(cmd.equalsIgnoreCase(BaseCommand.LoginCommand)){
            //请求对战
            
            baseMessage=new LoginMessage(this.ioSession,this.message);
        }else if(cmd.equalsIgnoreCase(BaseCommand.GuessTimeOverCommand)){
            //对家猜测超时
            
            baseMessage=new GuessTimeOverMessage(this.ioSession,this.message);
        }else if(cmd.equalsIgnoreCase(BaseCommand.OpenMatchCommand)){
            //对家开看结果
            
            baseMessage=new OpenMatchMessage(this.ioSession,this.message);
        }else if(cmd.equalsIgnoreCase(BaseCommand.QuitTeamCommand)){
            //对家退出对战
            
            baseMessage=new QuitTeamMessage(this.ioSession,this.message);
        }else if(cmd.equalsIgnoreCase(BaseCommand.StartShakeDiceCommand)){
            //开始摇骰子
            
            baseMessage=new ShakeDiceMessage(this.ioSession,this.message);
        }else if(cmd.equalsIgnoreCase(BaseCommand.StartGuessCommand)){
            //开始猜测
            
            baseMessage=new StartGuessMessage(this.ioSession,this.message);
        }else if(cmd.equalsIgnoreCase(BaseCommand.SendDiceNumbersToVSPlayerCommand)){
            //对家摇定的所有骰子点数
            
            baseMessage=new VSPlayerAllDiceNumberMessage(this.ioSession,this.message);
        }else if(cmd.equalsIgnoreCase(BaseCommand.SendGuessDataVSCommand)){
            //对家猜测的数据
            
            baseMessage=new VSPlayerGuessNumberMessage(this.ioSession,this.message);
        }else if(cmd.equalsIgnoreCase(BaseCommand.ResponsePKCommand)){
            //服务器返回的对战消息
            
            baseMessage=new ResponsePKMessage(this.ioSession,this.message);
        }
        
        if(baseMessage !=null){
            baseMessage.setRobot(this.robot);
            baseMessage.execute();;
        }
    }
}
